﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.TBKN;
using BLL.TBKN;

namespace WindowsForms.TBKN
{
    public partial class FormKhieuNai : Form
    {
        List<string> list = new List<string>();
        string maKhieuNai;
        public FormKhieuNai()
        {
            InitializeComponent();
            
        }


        private void LoadListViewKH(List<KhieuNai> listKN)
        {
            lvKhieuNai.Items.Clear();
            foreach (var kn in listKN)
            {
                ListViewItem lvitem = new ListViewItem(kn.MaKhieuNai);
                lvitem.SubItems.Add(kn.TenKhieuNai);
                lvitem.SubItems.Add(kn.NguoiGui);
                lvitem.SubItems.Add(kn.TenNguoiNhan);
                lvitem.SubItems.Add(kn.NgayGui.ToShortDateString());
                lvitem.SubItems.Add(kn.NgayTraLoi.ToShortDateString());
                lvitem.SubItems.Add(kn.TrangThai.ToString() == "2" ? "chưa trả lời" : "đã trả lời");
                lvitem.SubItems.Add(kn.NoiDung);
                lvKhieuNai.Items.Add(lvitem);
            }
        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            FormThemKhieuNai frm = new FormThemKhieuNai();
            frm.ShowDialog();
        }

        private void TimKiemtoolStripButton1_Click(object sender, EventArgs e)
        {
            FormTimKhieuNai frm = new FormTimKhieuNai();
            frm.FormClosed+= new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();
        }

        private void lvKhieuNai_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < lvKhieuNai.CheckedItems.Count; i++)
            {
                maKhieuNai = lvKhieuNai.CheckedItems[i].SubItems[0].Text;
                list.Add(maKhieuNai);
            }
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (var item in list)
            {
                
                if(KhieuNai.Objects.Delete(p=>p.MaKhieuNai==item))
                {
                    MessageBox.Show("Xóa thành công");
                    LoadListViewKH(KhieuNai.Objects.All().ToList());
                }
                else
                    MessageBox.Show("Xóa thất bại");
            }
        }

        private void LoadtoolStripButton1_Click(object sender, EventArgs e)
        {
            LoadListViewKH(KhieuNai.Objects.All().ToList());
        }


        public void frm_FormClosed(object sender, EventArgs e)
        {
            if(FormTimKhieuNai.state)
            {
                string dataSource = FormTimKhieuNai.data;
                
                if (!String.IsNullOrWhiteSpace(dataSource))
                {
                    List<KhieuNai> dskn = KhieuNai.Objects.Find(p => p.TenKhieuNai.Contains(dataSource)).ToList();
                    if (dskn.Count>0)
                    {
                        LoadListViewKH(dskn);
                    }
                    else
                        MessageBox.Show("Không tồn tại khiếu nại có tên: " + dataSource);
                }
               
            }
        }

        private void CapNhatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormThemKhieuNai frm = new FormThemKhieuNai();
            foreach (ListViewItem lvitem in lvKhieuNai.SelectedItems)
            {
                frm.mtxtMaKhieuNai.Text = lvitem.SubItems[0].Text;
                frm.mtxtMaKhieuNai.ReadOnly = true;
                frm.txtTenKhieuNai.Text = lvitem.SubItems[1].Text;
                frm.txtNguoiGui.Text = lvitem.SubItems[2].Text;
                frm.cbbNguoiNhan.Text = lvitem.SubItems[3].Text;
                frm.dtpNgayGui.Value = DateTime.Parse(lvitem.SubItems[4].Text);
                frm.dtpNgayTraLoi.Value = DateTime.Parse(lvitem.SubItems[5].Text);
                frm.cbbTrangThai.SelectedIndex = int.Parse(lvitem.SubItems[6].Text);
                frm.txtNoiDung.Text = lvitem.SubItems[7].Text;
            }
            //frm.btnUpdate.Enabled = true;
            frm.ShowDialog();
        }

        private void ThoatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void printToolStripButton_Click(object sender, EventArgs e)
        {
            InKhieuNai frm = new InKhieuNai();
            frm.ShowDialog();
        }

        
    }
}
